import example.ConfigMultiton;
import example.DatabaseConnectionMultiton;
import example.StateMultiton;
import multitionPattern.FixedMultiton;

import java.util.Map;

public class MultitonPatternDemo {
    
    public static void main(String[] args) {
        System.out.println("=== 多例模式演示 ===\n");
        
        // 测试固定数量多例
        testFixedMultiton();
        
        // 测试数据库连接池多例
        testDatabaseConnectionMultiton();
        
        // 测试状态机多例
        testStateMultiton();
        
        // 测试配置管理器多例
        testConfigMultiton();
    }
    
    private static void testFixedMultiton() {
        System.out.println("1. 固定数量多例测试:");
        
        for (int i = 1; i <= 3; i++) {
            FixedMultiton instance = FixedMultiton.getInstance(i);
            instance.doSomething();
        }
        
        // 验证实例唯一性
        FixedMultiton instance1 = FixedMultiton.getInstance(1);
        FixedMultiton instance2 = FixedMultiton.getInstance(1);
        System.out.println("实例1是否相同: " + (instance1 == instance2));
        System.out.println();
    }
    
    private static void testDatabaseConnectionMultiton() {
        System.out.println("2. 数据库连接池多例测试:");
        
        // 获取不同数据库的连接池实例
        DatabaseConnectionMultiton userDb = DatabaseConnectionMultiton.getInstance("user_db");
        DatabaseConnectionMultiton orderDb = DatabaseConnectionMultiton.getInstance("order_db");
        DatabaseConnectionMultiton productDb = DatabaseConnectionMultiton.getInstance("product_db");
        
        System.out.println("用户数据库可用连接: " + userDb.getAvailableConnections());
        System.out.println("订单数据库可用连接: " + orderDb.getAvailableConnections());
        System.out.println("产品数据库可用连接: " + productDb.getAvailableConnections());
        
        // 验证实例管理
        Map<String, DatabaseConnectionMultiton> allInstances = DatabaseConnectionMultiton.getAllInstances();
        System.out.println("总实例数量: " + allInstances.size());
        System.out.println();
    }
    
    private static void testStateMultiton() {
        System.out.println("3. 状态机多例测试:");
        
        // 获取所有状态实例
        Map<String, StateMultiton> allStates = StateMultiton.getAllStates();
        System.out.println("可用状态:");
        allStates.forEach((key, state) -> {
            System.out.println("  " + key + ": " + state.getDescription());
        });
        
        // 测试状态处理
        StateMultiton processingState = StateMultiton.getInstance("PROCESSING");
        processingState.handle();
        
        StateMultiton successState = StateMultiton.getInstance("SUCCESS");
        successState.handle();
        System.out.println();
    }
    
    private static void testConfigMultiton() {
        System.out.println("4. 配置管理器多例测试:");
        
        // 获取不同环境的配置
        ConfigMultiton devConfig = ConfigMultiton.getInstance("dev");
        ConfigMultiton testConfig = ConfigMultiton.getInstance("test");
        ConfigMultiton prodConfig = ConfigMultiton.getInstance("prod");
        
        System.out.println("开发环境数据库: " + devConfig.getProperty("database.url"));
        System.out.println("测试环境日志级别: " + testConfig.getProperty("log.level"));
        System.out.println("生产环境日志级别: " + prodConfig.getProperty("log.level"));
        
        // 环境切换演示
        ConfigMultiton.switchEnvironment("prod");
        System.out.println();
    }
}